module(..., package.seeall)

require "utils.table"
require "moaix.input.common"


----------------------------------------------------------------------------------------------------
-- Setup module signals, channel, and callback binding shortcut functions
----------------------------------------------------------------------------------------------------
SIGNALS = {LEFT_PRESS    = "mouse left press", 
           LEFT_RELEASE  = "mouse left release", 
           RIGHT_PRESS   = "mouse right press",
           RIGHT_RELEASE = "mouse right release",
           MOVE          = "mouse move"}
channel, bind_shortcuts = moaix.input.common.make_channel("mouse", SIGNALS)
table.copy(bind_shortcuts, _M)  -- make bind shortcuts available at module level

----------------------------------------------------------------------------------------------------
-- Module state variables
----------------------------------------------------------------------------------------------------
x     = 0      -- window x coordinate
y     = 0      -- window y coordinate
dx    = 0      -- delta x of last move event
dy    = 0      -- delta y of last move event
left  = false  -- left button pressed
right = false  -- right button pressed

----------------------------------------------------------------------------------------------------
-- Input handling functions
----------------------------------------------------------------------------------------------------
function left_button_handler(pressed)
    --[[ Called when the left mouse button is pressed or released. ]]
    left = pressed
    channel:emit(pressed and SIGNALS.LEFT_PRESS or SIGNALS.LEFT_RELEASE)
end

function right_button_handler(pressed)
    --[[ Called when the right mouse button is pressed or released. ]]
    right = pressed
    channel:emit(pressed and SIGNALS.RIGHT_PRESS or SIGNALS.RIGHT_RELEASE)
end

function movement_handler(new_x, new_y)
    --[[ Called when the mouse is moved inside MOAI's window. ]]
    dx = new_x - x
    dy = new_y - y
    x  = new_x
    y  = new_y
    channel:emit(SIGNALS.MOVE)
end

----------------------------------------------------------------------------------------------------
-- Link input handlers to MOAI's devices
----------------------------------------------------------------------------------------------------
function init()
    if MOAIInputMgr.device.pointer == nil then
        print("WARNING: Mouse unavailable.")
        return false
    else
        print("INFO: Setting up mouse input handlers...")
        MOAIInputMgr.device.mouseLeft:setCallback(left_button_handler)
        MOAIInputMgr.device.mouseRight:setCallback(right_button_handler)
        MOAIInputMgr.device.pointer:setCallback(movement_handler)
        return true
    end
end


--[[
----------------------------------------------------------------------------------------------------
-- Debug textbox showing mouse state
----------------------------------------------------------------------------------------------------
require "utils.moai.text"
local text = utils.moai.text

function debug_textbox(l, t, r, b)
    local textbox = text.make_textbox(text.default_font, "Mouse debug", l, t, r, b)
    local function update_textbox()
        local text
        while true do
            text = string.format("%d, %d", x, y)
            if left then text = text .. "\nleft" end 
            if right then text = text .. "\nright" end 
            textbox:setString(text)
            coroutine.yield()
        end
    end
    textbox.update_thread = MOAICoroutine.new()
    textbox.update_thread:run(update_textbox)
    return textbox
end
--]]
